<template>
  <!-- 弹框 -->
  <el-dialog title="审核" v-model="dialogVisible" width="50%" :before-close="handleClose">
    <el-form :model="formData" ref="formRef" :rules="rules" label-width="150px" class="mr-5">
      <el-form-item prop="percent" label="业主分佣百分比%">
        <el-input-number v-model="formData.percent" :min="0" :max="100">
          <template #prefix>
            <span>%</span>
          </template>
        </el-input-number>
      </el-form-item>
      <el-form-item prop="auditDesc" label="审核信息">
        <el-input v-model="formData.auditDesc" type="textarea" :rows="4" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="success" @click="handleAudit(AUDIT_STATUS_ENUM['成功'])">成功</el-button>
      <el-button type="danger" @click="handleAudit(AUDIT_STATUS_ENUM['失败'])">驳回</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { auditEndStatus } from "@/api/apply/audit";
import { AUDIT_STATUS_ENUM } from "./utils/enum";
const emit = defineEmits(["update:visible", "updateList"]);
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  row: {
    type: Object,
    default: () => {},
  },
});
const dialogVisible = ref(props.visible);
// 关闭弹框的逻辑
const handleClose = () => {
  dialogVisible.value = false;
  emit("update:visible", false);
  formData.value = {
    percent: "",
    auditDesc: "",
  };
};
watchEffect(() => {
  dialogVisible.value = props.visible;
});

const formData = ref({
  percent: 100,
  auditDesc: "",
});
const rules = ref({
  percent: [{ required: true, message: "请输入业主分佣百分比", trigger: "blur" }],
  auditDesc: [{ required: true, message: "请输入审核信息", trigger: "blur" }],
});
const formRef = ref(null);
const handleAudit = async (status) => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      const { id } = props.row;
      let params = {
        id,
        percent: formData.value.percent,
        auditDesc: formData.value.auditDesc,
        status,
      };
      try {
        const res = await auditEndStatus(params);
        ElMessage.success("审核完成");
        handleClose();
        emit("updateList");
      } catch (error) {
        console.log(error);
      }
    } else {
      console.log("error submit!!");
    }
  });
};
</script>

<style lang="scss" scoped>
:deep(.el-form-item__content) {
  margin-left: 0 !important;
}

.margin-top {
  margin-top: 20px;
}
</style>
